// 同时操纵多个mutation可以使用actions;
import {
  playMode
}
from '@/utils/config.js'
import {
  shuffle
}
from '@/utils/shuffle'
import {
  getUserLikeList,
  updateUserInfo
} from '@/request/api'
import {
  saveSearch,
  deleteSearch,
  clearSearch,
  saveOptions,
  savePlay,
  saveFavorite,
  deleteFavorite,
  removefavoriteAll,
  saveUserInfo,
  clearUserInfo,
  saveLikeSong,
  deleteLikeSong,
  removeLikeAll
} from '@/utils/cache'

// 查找当前播放列表中是否有这一首歌
function findIndex(list, song) {
  return list.findIndex((item) => {
    return item.id === song.id
  })
}
// 封装多个mutations:
export const selectPlay = function ({
  commit,
  state
}, {
  list,
  index
}) {
  commit('SET_SEQUENCE_LIST', list)
  if (state.mode === playMode.random) {
    // 如果是随机播放：
    let randomList = shuffle(list);
    commit('SET_PLAYLIST', randomList);
    // 找到顺序列表的那一首歌对应随机列表的那一首歌的索引
    index = findIndex(randomList, list[index])
  } else {
    commit('SET_PLAYLIST', list)
  }
  commit('SET_CURRENT_INDEX', index)
  commit('SET_FULL_SCREEN', true)
  commit('SET_PLAYING_STATE', true)
}
// 随机播放列表
export const randomPlay = function ({
  commit
}, {
  list
}) {
  console.log('开始随机播放列表');

  // 提交多个状态：先切换播放模式为随机,后格式化列表为随机
  commit('SET_PLAY_MODE', playMode.random)
  commit('SET_SEQUENCE_LIST', list)
  let randomList = shuffle(list)
  commit('SET_PLAYLIST', randomList)
  commit('SET_CURRENT_INDEX', 0)
  commit('SET_FULL_SCREEN', true)
  commit('SET_PLAYING_STATE', true)
}



// 插入一首歌曲：
export const insertSong = function ({
  commit,
  state
}, song) {
  // 因为不能改变state,所以使用slice方法先返回三种playlist的副本,currentIndex是值类型所以不需要深度拷贝
  let playList = state.playList.slice();
  let sequenceList = state.sequenceList.slice();
  let currentIndex = state.currentIndex;
  // 记录当前歌曲：
  let currentSong = playList[currentIndex];
  // 返回当前歌曲是否要要插入歌曲的索引
  let fpIndex = findIndex(playList, song);
  // 插入歌曲所以索引加一
  currentIndex++;
  // 插入这首歌到当前索引
  playList.splice(currentIndex, 0, song)
  // 如果已经包含了这首歌,则删除之前的
  if (fpIndex > -1) {
    // 如果当前播放的歌曲索引大于列表中的索引
    if (currentIndex > fpIndex) {
      playList.splice(fpIndex, 1)
      currentIndex--
    } else {
      playList.splice(fpIndex + 1, 1)
    }
  }
  // 顺序列表应该插入的位置
  let currentSIndex = findIndex(sequenceList, currentSong) + 1;
  let fsIndex = findIndex(sequenceList, song)
  sequenceList.splice(currentSIndex, 0, song)
  if (fsIndex > -1) {
    if (currentSIndex > fsIndex) {
      sequenceList.splice(fsIndex, 1)
    } else {
      sequenceList.splice(fsIndex + 1, 1)
    }
  }
  commit('SET_PLAYLIST', playList)
  commit('SET_SEQUENCE_LIST', sequenceList)
  commit('SET_CURRENT_INDEX', currentIndex)
  commit('SET_FULL_SCREEN', true)
  commit('SET_PLAYING_STATE', true)
}


export const saveSearchHistory = function ({
  commit
}, query) {
  commit('SET_SEARCH_HISTORY', saveSearch(query))
}


export const deleteSearchHistory = function ({
  commit
}, query) {
  commit('SET_SEARCH_HISTORY', deleteSearch(query))
}


// 点击垃圾桶，删除全部历史记录
export const clearSearchHistory = function ({
  commit
}) {
  commit('SET_SEARCH_HISTORY', clearSearch())
}

export const savePlayerOptions = function ({
  commit
}, opts) {
  commit('SET_PLAYER_OPTIONS', saveOptions(opts))
}

// 删除一条歌曲：
export const deleteSong = function ({
  commit,
  state
}, song) {
  // 因为不能改变state,所以使用slice方法先返回一个playlist的副本,currentIndex是值类型所以不需要
  let playList = state.playList.slice();
  let sequenceList = state.sequenceList.slice();
  let currentIndex = state.currentIndex;
  let pIndex = findIndex(playList, song)
  playList.splice(pIndex, 1)
  let sIndex = findIndex(sequenceList, song)
  sequenceList.splice(sIndex, 1)

  // 如果当前播放的歌曲的索引在被删除的索引之后，或者删除的是最后一首歌 则当前的索引则要--
  if (currentIndex > pIndex || currentIndex === playList.length) {
    currentIndex--
  }
  commit('SET_PLAYLIST', playList)
  commit('SET_SEQUENCE_LIST', sequenceList)
  commit('SET_CURRENT_INDEX', currentIndex)

  const playingState = playList.length > 0
  // 如果全部删完：
  // if (playingState) {
  //   commit('SET_PLAYING_STATE', false)
  // } else {
  //   commit('SET_PLAYING_STATE', true)
  // }
  commit('SET_PLAYING_STATE', playingState)
}

export const deleteSongList = function ({
  commit
}) {
  commit('SET_PLAYLIST', [])
  commit('SET_SEQUENCE_LIST', [])
  commit('SET_CURRENT_INDEX', -1)
  commit('SET_PLAYING_STATE', false)
}


// 添加歌曲到歌曲历史记录
export const savePlayHistory = function ({
  commit
}, song) {
  commit('SET_PLAY_HISTORY', savePlay(song))
}


// 收藏喜欢列表
export const saveFavoriteList = function ({
  commit
}, song) {
  commit('SET_FAVORITE_LIST', saveFavorite(song))
}

export const deleteFavoriteList = function ({
  commit
}, song) {
  commit('SET_FAVORITE_LIST', deleteFavorite(song))
}

export const deleteFavoriteAll = function ({
  commit
}) {
  commit('SET_FAVORITE_LIST', removefavoriteAll())
}


// 登录状态点击歌曲喜欢:
export const saveLikeList = function ({
  commit
}, song) {
  commit('SET_USER_LIKELIST', saveLikeSong(song))
}


export const deleteLikeList = function ({
  commit
}, song) {
  commit('SET_USER_LIKELIST', deleteLikeSong(song))
}



// 保存登录状态：
export const saveLoginInfo = function ({
  commit
}, info) {
  commit('SET_USER_INFO', saveUserInfo(info))

}

// 清空登录状态：
export const clearUserDate = function ({
  commit
}) {
  commit("SET_USER_INFO", clearUserInfo())
  commit("SET_USER_LIKELIST", removeLikeAll())
}

// 更新用户信息:
export const changeLoginInfo = async function ({
  commit
}, info) {
  const res = await updateUserInfo(info);
  console.log(res);
  if (res.code == 200) {
    console.log(info);
    commit('SET_USER_INFO', saveUserInfo(info))
  }
}
